package com.orange.score.activity.league;

import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;
import com.orange.score.activity.common.CommonFootballActivity;
import com.orange.score.app.ScoreApplication;
import com.orange.score.model.league.League;
import com.orange.score.model.league.LeagueManager;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SelectLeagueActivity extends CommonFootballActivity
{
  public static final int ACTION_SELECT_LEAGUE = 20120109;
  private static final int COLUMN_MARGIN = 3;
  public static final String KEY_LEAGUE_FROM = "league_from";
  public static final String KEY_SELECTED_LEAGUE = "selected_league";
  private static final int LEAGUE_BUTTON_HEIGHT = 28;
  public static final String SELECTLEAGUE_TAG = "SelectLeagueActivity";
  protected static final String TAG = "SelectLeagueActivity";
  private static Set<String> inputSelectedSet;
  private static Set<League> selectedSet = new HashSet();
  private int allMatchCnt;
  private Button allSelect_button;
  private Button[] buttons;
  private int hiddenMatchCnt;
  private List<League> leagueList;
  private int listSize;
  private Button noSelect_button;
  private TableLayout tableLayout;
  private TextView textView;
  private int topMatchCnt;
  private Button topSelect_button;
  int totalMatchCount;

  static
  {
    inputSelectedSet = new HashSet();
  }

  private int calculateAllMatchCount(List<League> paramList)
  {
    int i = 0;
    int j = paramList.size();
    int k = 0;
    while (true)
    {
      if (k >= j)
        return i;
      i += ((League)paramList.get(k)).matchCount;
      ++k;
    }
  }

  private int getCurrentHiddenMatchCount()
  {
    int i;
    int j;
    int k;
    Object[] arrayOfObject = selectedSet.toArray();
    if (arrayOfObject == null)
      k = 0;
    while (true)
    {
      return k;
      i = 0;
      j = 0;
      if (j < arrayOfObject.length)
        break;
      k = this.totalMatchCount - i;
    }
    if (arrayOfObject[j] == null);
    while (true)
    {
      while (true)
        ++j;
      i += ((League)arrayOfObject[j]).matchCount;
    }
  }

  private ArrayList<String> getSelectedLeagueIdArray()
  {
    ArrayList localArrayList = new ArrayList();
    Object[] arrayOfObject = selectedSet.toArray();
    int i = 0;
    while (true)
    {
      if (i >= arrayOfObject.length)
        return localArrayList;
      localArrayList.add(((League)arrayOfObject[i]).leagueId);
      ++i;
    }
  }

  private void initInputSelectedList()
  {
    int j;
    int k;
    new Bundle();
    Bundle localBundle = getIntent().getExtras();
    int i = localBundle.getInt("league_from");
    LeagueManager localLeagueManager = ((ScoreApplication)getApplication()).getLeagueManager(i);
    this.leagueList = localLeagueManager.findAllLeagues();
    this.totalMatchCount = calculateAllMatchCount(this.leagueList);
    Log.d("SelectLeagueActivity", "total league match count = " + this.totalMatchCount);
    ArrayList localArrayList = localBundle.getStringArrayList("selected_league");
    inputSelectedSet.clear();
    if (localArrayList != null)
    {
      inputSelectedSet.addAll(localArrayList);
      j = localArrayList.size();
      selectedSet.clear();
      k = 0;
    }
    while (true)
    {
      if (k >= j)
        return;
      League localLeague = localLeagueManager.findLeagueById((String)localArrayList.get(k));
      selectedSet.add(localLeague);
      ++k;
    }
  }

  private void setHiddenTextView(int paramInt)
  {
    int i = getCurrentHiddenMatchCount();
    this.textView.setText(String.valueOf(i));
  }

  private void updateButtonTextColor()
  {
    Button[] arrayOfButton = this.buttons;
    int i = arrayOfButton.length;
    int j = 0;
    if (j >= i)
      return;
    Button localButton = arrayOfButton[j];
    if (localButton.isSelected())
      localButton.setTextColor(getResources().getColor(2131230751));
    while (true)
    {
      while (true)
        ++j;
      localButton.setTextColor(-16777216);
    }
  }

  public void onCreate(Bundle paramBundle)
  {
    int i;
    int j;
    1 local1;
    int i2;
    super.onCreate(paramBundle);
    requestWindowFeature(1);
    setContentView(2130903100);
    initInputSelectedList();
    this.tableLayout = ((TableLayout)findViewById(2131558643));
    this.tableLayout.setStretchAllColumns(true);
    this.textView = ((TextView)findViewById(2131558664));
    this.listSize = this.leagueList.size();
    if (this.listSize % 4 == 0)
    {
      i = this.listSize / 4;
      this.buttons = new Button[i * 4];
      j = 0;
      if (j < i)
        break label323;
      setHiddenTextView(this.hiddenMatchCnt);
      updateButtonTextColor();
      local1 = new View.OnClickListener(this)
      {
        public void onClick()
        {
          boolean bool;
          Button localButton = (Button)paramView;
          SelectLeagueActivity.access$0(this.this$0);
          League localLeague = (League)localButton.getTag();
          if (localLeague == null)
          {
            Log.e("SelectLeagueActivity", "League of button not found!");
            return;
          }
          int i = localLeague.matchCount;
          if (localButton.isSelected())
          {
            SelectLeagueActivity.access$1().remove(localLeague);
            SelectLeagueActivity localSelectLeagueActivity2 = this.this$0;
            SelectLeagueActivity.access$3(localSelectLeagueActivity2, SelectLeagueActivity.access$2(localSelectLeagueActivity2) - i);
            if (!(localButton.isSelected()))
              break label147;
            bool = false;
          }
          while (true)
          {
            while (true)
            {
              while (true)
              {
                localButton.setSelected(bool);
                SelectLeagueActivity.access$4(this.this$0, SelectLeagueActivity.access$2(this.this$0));
                SelectLeagueActivity.access$0(this.this$0);
              }
              SelectLeagueActivity.access$1().add(localLeague);
              SelectLeagueActivity localSelectLeagueActivity1 = this.this$0;
              SelectLeagueActivity.access$3(localSelectLeagueActivity1, i + SelectLeagueActivity.access$2(localSelectLeagueActivity1));
            }
            label147: bool = true;
          }
        }
      };
      i2 = 0;
    }
    while (true)
    {
      label323: TableRow localTableRow;
      TableRow.LayoutParams localLayoutParams;
      int k;
      while (true)
      {
        while (true)
        {
          int i3 = this.listSize;
          if (i2 < i3)
            break label732;
          Button localButton2 = (Button)findViewById(2131558660);
          localButton2.setBackgroundResource(2130837638);
          2 local2 = new View.OnClickListener(this)
          {
            public void onClick()
            {
              if ((SelectLeagueActivity.access$1() == null) || (SelectLeagueActivity.access$1().size() == 0))
                Toast.makeText(this.this$0.getApplicationContext(), "至少要选择一个赛事", 1).show();
              while (true)
              {
                return;
                Intent localIntent = new Intent();
                ArrayList localArrayList = SelectLeagueActivity.access$5(this.this$0);
                Log.i("SelectLeagueActivity", "selectedLeagues=" + localArrayList.toString());
                Bundle localBundle = new Bundle();
                localBundle.putStringArrayList("selected_league", localArrayList);
                localIntent.putExtras(localBundle);
                this.this$0.setResult(20120109, localIntent);
                this.this$0.finish();
              }
            }
          };
          localButton2.setOnClickListener(local2);
          this.allSelect_button = ((Button)findViewById(2131558662));
          this.allSelect_button.setBackgroundResource(2130837638);
          3 local3 = new View.OnClickListener(this)
          {
            public void onClick()
            {
              int j;
              int i = 0;
              if (i >= SelectLeagueActivity.access$6(this.this$0))
                j = 0;
              while (true)
              {
                while (j >= SelectLeagueActivity.access$6(this.this$0))
                {
                  ((Button)paramView).setSelected(true);
                  SelectLeagueActivity.access$9(this.this$0).setSelected(false);
                  SelectLeagueActivity.access$10(this.this$0).setSelected(false);
                  SelectLeagueActivity.access$3(this.this$0, 0);
                  SelectLeagueActivity.access$4(this.this$0, SelectLeagueActivity.access$2(this.this$0));
                  SelectLeagueActivity.access$0(this.this$0);
                  return;
                  SelectLeagueActivity.access$7(this.this$0)[i].setSelected(true);
                  ++i;
                }
                League localLeague = (League)SelectLeagueActivity.access$8(this.this$0).get(j);
                SelectLeagueActivity.access$1().add(localLeague);
                ++j;
              }
            }
          };
          this.allSelect_button.setOnClickListener(local3);
          this.noSelect_button = ((Button)findViewById(2131558661));
          this.noSelect_button.setBackgroundResource(2130837638);
          4 local4 = new View.OnClickListener(this)
          {
            public void onClick()
            {
              int i = 0;
              while (true)
              {
                if (i >= SelectLeagueActivity.access$6(this.this$0))
                {
                  SelectLeagueActivity.access$1().clear();
                  ((Button)paramView).setSelected(true);
                  SelectLeagueActivity.access$11(this.this$0).setSelected(false);
                  SelectLeagueActivity.access$10(this.this$0).setSelected(false);
                  SelectLeagueActivity.access$4(this.this$0, SelectLeagueActivity.access$12(this.this$0));
                  SelectLeagueActivity.access$3(this.this$0, SelectLeagueActivity.access$12(this.this$0));
                  SelectLeagueActivity.access$0(this.this$0);
                  return;
                }
                SelectLeagueActivity.access$7(this.this$0)[i].setSelected(false);
                ++i;
              }
            }
          };
          this.noSelect_button.setOnClickListener(local4);
          this.topSelect_button = ((Button)findViewById(2131558663));
          this.topSelect_button.setBackgroundResource(2130837638);
          5 local5 = new View.OnClickListener(this)
          {
            public void onClick()
            {
              ((Button)paramView).setSelected(true);
              SelectLeagueActivity.access$9(this.this$0).setSelected(false);
              SelectLeagueActivity.access$11(this.this$0).setSelected(false);
              int i = 0;
              if (i >= SelectLeagueActivity.access$6(this.this$0))
              {
                SelectLeagueActivity.access$4(this.this$0, SelectLeagueActivity.access$12(this.this$0) - SelectLeagueActivity.access$13(this.this$0));
                SelectLeagueActivity.access$0(this.this$0);
                return;
              }
              League localLeague = (League)SelectLeagueActivity.access$8(this.this$0).get(i);
              if (localLeague.isTop)
              {
                SelectLeagueActivity.access$7(this.this$0)[i].setSelected(true);
                SelectLeagueActivity.access$1().add(localLeague);
              }
              while (true)
              {
                while (true)
                  ++i;
                SelectLeagueActivity.access$7(this.this$0)[i].setSelected(false);
                SelectLeagueActivity.access$1().remove(localLeague);
              }
            }
          };
          this.topSelect_button.setOnClickListener(local5);
          return;
          i = 1 + this.listSize / 4;
        }
        localTableRow = new TableRow(this);
        localLayoutParams = new TableRow.LayoutParams(-2, -2, 1F);
        localLayoutParams.width = 1;
        localLayoutParams.setMargins(3, 3, 3, 3);
        this.tableLayout.setLayoutParams(localLayoutParams);
        k = 0;
        if (k < 4)
          break;
        this.tableLayout.addView(localTableRow);
        ++j;
      }
      int l = k + j * 4;
      Button[] arrayOfButton = this.buttons;
      Button localButton1 = new Button(this);
      arrayOfButton[l] = localButton1;
      if (l >= this.listSize)
      {
        this.buttons[l].setLayoutParams(localLayoutParams);
        localTableRow.addView(this.buttons[l]);
        this.buttons[l].setVisibility(4);
        this.buttons[l].setLayoutParams(localLayoutParams);
        this.buttons[l].setHeight(28);
        this.buttons[l].setBackgroundResource(2130837640);
      }
      while (true)
      {
        while (true)
          ++k;
        League localLeague = (League)this.leagueList.get(l);
        int i1 = localLeague.getMatchCount();
        if (localLeague.isTop)
          this.topMatchCnt = (i1 + this.topMatchCnt);
        this.hiddenMatchCnt = (i1 + this.hiddenMatchCnt);
        this.allMatchCnt = (i1 + this.allMatchCnt);
        String.valueOf(i1);
        String str = localLeague.getLeagueName();
        this.buttons[l].setText(str);
        this.buttons[l].setLayoutParams(localLayoutParams);
        this.buttons[l].setSingleLine(true);
        this.buttons[l].setTextSize(13.0F);
        this.buttons[l].setBackgroundResource(2130837640);
        this.buttons[l].setHeight(28);
        this.buttons[l].setTag(localLeague);
        if (inputSelectedSet.contains(localLeague.leagueId))
        {
          this.hiddenMatchCnt -= localLeague.getMatchCount();
          this.buttons[l].setSelected(true);
        }
        localTableRow.addView(this.buttons[l]);
      }
      label732: this.buttons[i2].setOnClickListener(local1);
      ++i2;
    }
  }

  protected void onResume()
  {
    int i;
    super.onResume();
    if ((this.buttons != null) && (this.buttons.length > 0))
      i = 0;
    while (true)
    {
      if (i >= this.buttons.length)
        return;
      updateButtonTextColor();
      ++i;
    }
  }
}